package com.zhengjiang.pattern.demo.singlton;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SingleTest {
    public static void main(String[] args) throws InterruptedException {
       test3();
    }

    static void test1(){
        Set set = new HashSet();
        SingleObject1 instance1 = SingleObject1.getInstance();
        SingleObject1 instance2 = SingleObject1.getInstance();
        SingleObject1 instance3 = SingleObject1.getInstance();
        set.add(instance1);
        set.add(instance2);
        set.add(instance3);
        System.out.println("set.size = "+set.size());
    }

    static void test2(){
        Set set = new HashSet();
        for(int i=0;i<5;i++){
            new Thread(()->{
                SingleObject1 instance = SingleObject1.getInstance();
                set.add(instance);
            }).start();
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("set.size = "+set.size());
    }

    static void test3(){
        Set<SingleObject2> set = new HashSet<>();
        for(int i=0;i<5;i++){
            new Thread(()->{
                SingleObject2 instance = SingleObject2.getInstance();
                set.add(instance);
            }).start();
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("set.size = "+set.size());

      /*  for (int i=0;i<set.size();i++){
            System.out.println(i);
        }*/

        Iterator<SingleObject2> iterator = set.iterator();
        while (iterator.hasNext()){
            SingleObject2 next = iterator.next();
            System.out.println(next);
        }

//        set.stream().forEach(s-> System.out.println(s));
//        System.out.println("set.size = "+set.size());

    }
}
